package com.ihomey.linkuphome.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.entities.SingleDevice;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;


/**
 * Created by Administrator on 2016/12/28.
 */

public class AddedDeviceAdapter extends SwipeMenuAdapter<AddedDeviceAdapter.ListHolder> {
    //获取从Activity中传递过来每个item的数据集合
    private List<SingleDevice> mDatas;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemLongClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    //构造函数
    public AddedDeviceAdapter(List<SingleDevice> list) {
        this.mDatas = list;
    }

    @Override
    public void onBindViewHolder(ListHolder holder, int position) {
        holder.tv.setText(mDatas.get(position).getName() + "("+holder.tv.getContext().getResources().getString(R.string.state_added)+")");
        holder.setOnItemClickListener(mOnItemClickListener);
        return;

    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device_added, parent, false);
        return layout;
    }

    @Override
    public ListHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ListHolder(realContentView);
    }


    //在这里面加载ListView中的每个item的布局
    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView tv;
        OnItemClickListener mOnItemClickListener;

        public ListHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_device_name);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        @Override
        public void onClick(View v) {
//

        }

        @Override
        public boolean onLongClick(View v) {
            if (mOnItemClickListener != null) {
                v.setTag(getAdapterPosition() + "");
                mOnItemClickListener.onItemLongClick(v, getAdapterPosition());
            }
            return true;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas!=null?mDatas.size():0;
    }

    public SingleDevice getItem(int position){
        return mDatas!=null?mDatas.get(position):null;
    }
}
